﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ShredTheLog.Api;
using System.ComponentModel.Composition;
using ShredTheLog.Common.MVVM;

namespace ShredTheLog.ViewModels
{
    [Export(typeof(ISettingsPageViewModel))]
    [OrderMetadata(Order = 100)]
    [PartCreationPolicy(CreationPolicy.NonShared)] // Модель должна пересоздаваться, чтобы после закрытия окна настроек по отмене окно настроек при повторном открытии отображало актуальные значения настроек, а не отмененные в ходе предыдущего сеанса
    public sealed class ConnectionSettingsPageViewModel : ViewModelBase, ISettingsPageViewModel
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        [Import]
        private IConnectionSettingsSection ConnectionSettings { get; set; }

        public ConnectionSettingsPageViewModel()
        {
            Servers = new ReadOnlyCollection<string>(new string[] 
            { 
                "http://sharedtalk.net", 
                "http://sharedtalk.com", 
                "http://anonymouse.org/cgi-bin/anon-www.cgi/http://sharedtalk.com",
                "http://213.171.222.179"
            });
            UseDefaultConnectionSettings = ConnectionSettings.UseDefaultConnectionSettings;
            TextChatServer = ConnectionSettings.TextChatServer;
            BaseServer = ConnectionSettings.BaseServer;
        }

        public string Header { get { return ShredServices.GetLocalizedString("s_Connection"); } }
        public ReadOnlyCollection<string> Servers { get; private set; }
        public bool UseDefaultConnectionSettings { get; set; }
        public string TextChatServer { get; set; }
        public string BaseServer { get; set; }

        public void Save()
        {
            ConnectionSettings.UseDefaultConnectionSettings = UseDefaultConnectionSettings;
            ConnectionSettings.TextChatServer = TextChatServer;
            ConnectionSettings.BaseServer = BaseServer;
        }
    }
}
